package net.jalbum.filters;

import java.awt.event.ActionEvent;

import javax.swing.JLabel;
import javax.swing.JTextField;

import se.datadosen.jalbum.Msg;

public class MultipleResizableCircleControl extends	MultipleResizableObjectControl {
	/**
	 *
	 */
	private static final long serialVersionUID = 7586624283212259387L;
	protected MultipleResizableCirclePanel multipleResizableCirclePanel;
	protected double Radius = 0.2;
	protected double Radius2 = 0.1;
	protected JTextField textX;
	protected JTextField textY;
	protected JTextField textRadius;
	protected JTextField textRadius2;

	public MultipleResizableCircleControl() {
	}

	public MultipleResizableCircleControl(MultipleResizableCirclePanel multipleResizableCirclePanel) {
		init(multipleResizableCirclePanel);
		addMultipleControls();
	}

	protected void addCircleControls(){
		textX = new JTextField(3);
		textY = new JTextField(3);
		textRadius = new JTextField(3);
		textRadius2 = new JTextField(3);

		textX.addActionListener(this);
		textY.addActionListener(this);
		textRadius.addActionListener(this);
		textRadius2.addActionListener(this);

		this.add("p",new JLabel(Msg.getString(this,"filters.focus.positionX")));
		this.add(textX);

		this.add("p",new JLabel(Msg.getString(this,"filters.focus.positionY")));
		this.add(textY);

		this.add("p",new JLabel(Msg.getString(this,"filters.focus.radius")));
		this.add(textRadius);

		if(multipleResizableCirclePanel.isShowRadius2()){
			this.add("p",new JLabel(Msg.getString(this,"filters.focus.radius2")));
			this.add(textRadius2);
		}
	}

	protected void init(MultipleResizableCirclePanel multipleResizableCirclePanel) {
		this.multipleResizableCirclePanel = multipleResizableCirclePanel;

		super.init(multipleResizableCirclePanel);
	}

	public void update(double x,double y, double radius, double radius2){
		this.X1 = x;
		textX.setText(Double.toString(x));
		this.Y1 =y;
		textY.setText(Double.toString(y));
		this.Radius = radius;
		textRadius.setText(Double.toString(radius));
		this.Radius2 = radius2;
		textRadius2.setText(Double.toString(radius2));
	}

	public void actionPerformed(ActionEvent e) {
		super.actionPerformed(e);
		if (e.getSource()== textX){
			try{
				//parse input
				int x = Integer.parseInt(textX.getText());
				//check values
				if(x<0){
					x=0;
				}else if(x>objectWidth-1){
					x=objectWidth-1;
				}
				this.X1=x/(double)(objectWidth-1);
			}catch(Exception exception){}
		}else if (e.getSource()== textY){
			try{
				//parse input
				int y = Integer.parseInt(textY.getText());
				//check values
				if(y<0){
					y=0;
				}else if(y>objectHeight-1){
					y=objectHeight-1;
				}
				this.Y1=y/(double)(objectHeight-1);
			}catch(Exception exception){}
		}else if (e.getSource()== textRadius){
			try{
				//parse input
				int radius = Integer.parseInt(textRadius.getText());
				//check values
				if(radius<0){
					radius=Math.abs(radius);
				}
				if(radius>Math.max(objectWidth, objectHeight)-1){
					radius=Math.max(objectWidth, objectHeight)-1;
				}
				this.Radius=radius/(double)(objectWidth-1);
			}catch(Exception exception){}
		}else if (e.getSource()== textRadius2){
			try{
				//parse input
				int radius2 = Integer.parseInt(textRadius2.getText());
				//check values
				if(radius2<0){
					radius2=Math.abs(radius2);
				}
				if(radius2>Radius*Math.max(objectWidth-1, objectHeight-1)){
					this.Radius2=Math.max(objectWidth-1, objectHeight-1)/(double)(objectWidth-1);
				}else{
					this.Radius2=radius2;					
				}
			}catch(Exception exception){}
		}

		if((e.getSource()==textX || e.getSource()==textY || e.getSource()==textRadius || e.getSource()==textRadius2) && !multipleResizableCirclePanel.getResizableCircle().isInitial()){
			textX.setText(Integer.toString((int)(X1*(objectWidth-1))));
			textY.setText(Integer.toString((int)(Y1*(objectHeight-1))));
			textRadius.setText(Integer.toString((int)(Radius*(objectWidth-1))));
			textRadius2.setText(Integer.toString((int)(Radius2*(objectWidth-1))));
			multipleResizableCirclePanel.updateCircle(X1, Y1, Radius, Radius2);
		}
		if((e.getSource()==removeButton || e.getSource()==nextButton || e.getSource()==previousButton) && !multipleResizableCirclePanel.getResizableCircle().isInitial()){
			textX.setText(Integer.toString((int)(multipleResizableCirclePanel.getResizableCircle().getX1())*(objectWidth-1)));
			textY.setText(Integer.toString((int)(multipleResizableCirclePanel.getResizableCircle().getY1())*(objectHeight-1)));
			textRadius.setText(Integer.toString((int)(multipleResizableCirclePanel.getResizableCircle().getRadius())*(objectWidth-1)));
			textRadius2.setText(Integer.toString((int)(multipleResizableCirclePanel.getResizableCircle().getRadius2())*(objectWidth-1)));
		}
	}

	public MultipleResizableCirclePanel getMultipleResizableCirclePanel() {
		return multipleResizableCirclePanel;
	}

	public void setMultipleResizableCirclePanel(
			MultipleResizableCirclePanel multipleResizableCirclePanel) {
		this.multipleResizableCirclePanel = multipleResizableCirclePanel;
	}

	public double getRadius() {
		return Radius;
	}

	public void setRadius(double radius) {
		this.update(X1, Y1, radius, Radius2);
	}

	public double getRadius2() {
		return Radius2;
	}

	public void setRadius2(double radius2) {
		this.update(X1, Y1, Radius, radius2);
	}
}
